//
//  LoadConfig.m
//  LoanOne
//
//  Created by WangZhenyu on 2017/10/5.
//  Copyright © 2017年 WangZhenyu. All rights reserved.
//

#import "LoadConfig.h"
#import "CacheData.h"

#define APPCONFIG                  @"APPCONFIG"
#define CATEGORIES                 @"CATEGORIES"
#define BANKAJINDU                 @"BANKAJINDU"
#define PERSON                     @"PERSON"
#define TOOLS                      @"TOOLS"
#define ABOUTOUR                   @"ABOUTOUR"

@implementation LoadConfig

+ (instancetype)shareStance
{
    static dispatch_once_t onceToken;
    static LoadConfig* shared = nil;
    dispatch_once(&onceToken, ^{
        shared = [[LoadConfig alloc] init];
    });
    return shared;
}

- (void)loadConfigSuccess:(void (^)(NSDictionary *))success failure:(void (^)(NSString *))fail{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:@"iOS" forKey:@"deviceType"];
    [dic setObject:@"AppStore" forKey:@"channel"];
    [dic setObject:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"] forKey:@"appName"];
    [dic setObject:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] forKey:@"version"];
    [[ServiceRequest sharedService] GET:@"appconfiguration" parameters:dic success:^(NSDictionary *responseObject) {
        NSDictionary *dic = [CacheData readApplicationDic:APPCONFIG];
        if (![responseObject isEqualToDictionary:dic]) {
            [CacheData writeApplicationData:responseObject writeFileName:APPCONFIG];
            success(responseObject);
        }else{
            success(dic);
        }
    } failure:^(NSString *error, NSInteger code) {
        NSDictionary *dic = [CacheData readApplicationDic:APPCONFIG];
        if (dic.count > 0) {
            success(dic);
        }else{
            fail(@"请检查您的网络情况后重新打开APP");
        }
    }];
}

- (NSDictionary *)getConfig{
    return [CacheData readApplicationDic:APPCONFIG];
}

- (void)loadCategoriesSuccess:(void (^)(NSArray *))success failure:(void (^)(NSString *))fail{
    [[ServiceRequest sharedService] GET:@"categories" parameters:nil success:^(id responseObject) {
        NSArray *arr = [CacheData readApplicationArr:CATEGORIES];
        if (![responseObject isEqualToArray:arr]) {
            [CacheData writeApplicationData:responseObject writeFileName:CATEGORIES];
            success(responseObject);
        }else{
            success(arr);
        }
    } failure:^(NSString *error, NSInteger code) {
        NSArray *arr = [CacheData readApplicationArr:CATEGORIES];
        if (arr.count > 0) {
            success(arr);
        }else{
            fail(@"请检查您的网络情况后重新打开APP");
        }
    }];
}

- (NSArray *)getCategories{
    return [CacheData readApplicationArr:CATEGORIES];
}

- (void)loadBankajinduSuccess:(void (^)(NSDictionary *))success{
    [[ServiceRequest sharedService] GET:@"config-data" parameters:@{@"dataUrl":[NSString stringWithFormat:@"%@%@", HOSTURL, @"bawangdai/config/bankajindu.json"]} success:^(NSDictionary *responseObject) {
        NSDictionary *dic = [CacheData readApplicationDic:BANKAJINDU];
        if (![responseObject isEqualToDictionary:dic]) {
            [CacheData writeApplicationData:responseObject writeFileName:BANKAJINDU];
            success(responseObject);
        }
    } failure:^(NSString *error, NSInteger code) {
    }];
}

- (NSDictionary *)getBankajindu{
    return [CacheData readApplicationDic:BANKAJINDU];
}

- (void)loadPersonSuccess:(void (^)(NSDictionary *))success{
    [[ServiceRequest sharedService] GET:@"config-data" parameters:@{@"dataUrl":[NSString stringWithFormat:@"%@%@", HOSTURL, @"bawangdai/config/person.json"]} success:^(NSDictionary *responseObject) {
        NSDictionary *dic = [CacheData readApplicationDic:PERSON];
        if (![responseObject isEqualToDictionary:dic]) {
            [CacheData writeApplicationData:responseObject writeFileName:PERSON];
            success(responseObject);
        }
    } failure:^(NSString *error, NSInteger code) {
    }];
}

- (NSDictionary *)getPerson{
    return [CacheData readApplicationDic:PERSON];
}

- (void)loadToolsSuccess:(void (^)(NSDictionary *))success{
    [[ServiceRequest sharedService] GET:@"config-data" parameters:@{@"dataUrl":[NSString stringWithFormat:@"%@%@", HOSTURL, @"bawangdai/config/tools.json"]} success:^(NSDictionary *responseObject) {
        NSDictionary *dic = [CacheData readApplicationDic:TOOLS];
        if (![responseObject isEqualToDictionary:dic]) {
            [CacheData writeApplicationData:responseObject writeFileName:TOOLS];
            success(responseObject);
        }
    } failure:^(NSString *error, NSInteger code) {
        NSDictionary *dic = [CacheData readApplicationDic:TOOLS];
        if (dic.count > 0) {
            
        }else{
        }
    }];
}

- (NSDictionary *)getTools{
    return [CacheData readApplicationDic:TOOLS];
}

- (void)loadAboutSuccess:(void (^)(NSDictionary *))success{
    [[ServiceRequest sharedService] GET:@"config-data" parameters:@{@"dataUrl":[NSString stringWithFormat:@"%@%@", HOSTURL, @"bawangdai/config/about.json"]} success:^(NSDictionary *responseObject) {
        NSDictionary *dic = [CacheData readApplicationDic:ABOUTOUR];
        if (![responseObject isEqualToDictionary:dic]) {
            [CacheData writeApplicationData:responseObject writeFileName:ABOUTOUR];
            success(responseObject);
        }
    } failure:^(NSString *error, NSInteger code) {

    }];
}

- (NSDictionary *)getAbout{
    return [CacheData readApplicationDic:ABOUTOUR];
}
@end
